<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_sliderMenuItemSortAction extends f_core_Action
{
	public function execute()
	{		
		$docId = $this->getRequestParameter("docId");
		$docLang = $this->getRequestParameter("docLang");		
		$nodes = $this->getRequestParameter("slideNode");

        if($this->getRequestParameter('href') == f_core_Routing::getUrl('modulesRoot'))
        {
            $this->getUser()->setExtraParameter('modulesOrder', $nodes)->save();
        }
        else
        {
            if(is_null($docLang))
            {
                $docLang = $this->getContextLang();
            }
            
            $node = $this->getDocumentProvider()->getByUniqueId($nodes[0],$docLang);
            $parent = f_relation_ManagerNew::getInstance($node)->getParents()->withLang($docLang)->limit(1)->retrieveDocuments();
            
            if($parent[0]->getUniqueId() != $docId)
            {
                $docId = $parent[0]->getUniqueId();
                $docLang = $parent[0]->getLang();
            }

            f_relation_ManagerNew::getInstance(null)->orderRelations($docId, $docLang, $nodes);

            f_template_Template::getInstance()->clear_cache(null, strtoupper($docLang)."|page");
        }
		
		return null;
	}	
	
	public function validate()
	{
        if($this->getRequestParameter("href") == f_core_Routing::getUrl('modulesRoot'))
        {
            return $this->getUserContext()->hasRight(array("backoffice_accessBo"));
        }
		f_core_Routing::getInstance()->setRequestURL($this->getRequestParameter("href"));
		return $this->getUserContext()->hasRight(array("backoffice_accessBo", $this->getRequestParameter("module")."_Edit"));
	}
	
	public function errorHandler()
	{
		return null;
	}
}